﻿using System;
using System.Collections.Generic;
using System.Text;

namespace eTMS.Commons
{
    public class ConverterFont
    {
        public enum FontType
        {
            None,
            TCVN3,
            VRE
        }

        private static char[] vrechars = {
        '§','ª','¨','©','«',
        ' ','¬','¯','­','®','µ',
        '¡','¶','½','·','¸','¾',
        '¦','Æ','Ð','Ó','Ô','Ö',
        '×','Ý','Ø','Ü','Þ',
        'ß','ã','á','â','ä',
        '£','å','è','æ','ç','é',
        '¤','ê','í','ë','ë','ì','î',
        'ï','ó','ñ','ò','ô',
        '¥','õ','ø','ö','÷','ù',
        'ú','ý','û','ü','þ',
        '™','š','Ÿ','›','œ','','ž'
    };
        // '™','š','Ÿ','›','œ','','ž'
        //'™','š','¥','›','ø','','ž'

        private static char[] ascchars = {
        'a', 'a', 'a', 'a', 'a', 
        'a', 'a', 'a', 'a', 'a', 'a', 
        'a', 'a', 'a', 'a', 'a', 'a', 
        'd', 'e', 'e', 'e', 'e', 'e', 
        'e', 'e', 'e', 'e', 'e', 'e', 
        'i', 'i', 'i', 'i', 'i', 
        'o', 'o', 'o', 'o', 'o', 
        'o', 'o', 'o', 'o', 'o', 'o', 
        'o', 'o', 'o', 'o', 'o', 'o', 
        'u', 'u', 'u', 'u', 'u', 
        'u', 'u', 'u', 'u', 'u', 'u', 
        'y', 'y', 'y', 'y', 'y', 
        'A', 'A', 'D', 'E', 'O', 'O', 'U'
    };

        private static char[] tcvnchars = {
        'µ', '¸', '¶', '·', '¹', 
        '¨', '»', '¾', '¼', '½', 'Æ', 
        '©', 'Ç', 'Ê', 'È', 'É', 'Ë', 
        '®', 'Ì', 'Ð', 'Î', 'Ï', 'Ñ', 
        'ª', 'Ò', 'Õ', 'Ó', 'Ô', 'Ö', 
        '×', 'Ý', 'Ø', 'Ü', 'Þ', 
        'ß', 'ã', 'á', 'â', 'ä', 
        '«', 'å', 'è', 'æ', 'ç', 'é', 
        '¬', 'ê', 'í', 'ë', 'ì', 'î', 
        'ï', 'ó', 'ñ', 'ò', 'ô', 
        '­', 'õ', 'ø', 'ö', '÷', 'ù', 
        'ú', 'ý', 'û', 'ü', 'þ', 
        '¡', '¢', '§', '£', '¤', '¥', '¦'
    };
        private static char[] unichars = {
        'à', 'á', 'ả', 'ã', 'ạ', 
        'ă', 'ằ', 'ắ', 'ẳ', 'ẵ', 'ặ', 
        'â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ', 
        'đ', 'è', 'é', 'ẻ', 'ẽ', 'ẹ', 
        'ê', 'ề', 'ế', 'ể', 'ễ', 'ệ', 
        'ì', 'í', 'ỉ', 'ĩ', 'ị', 
        'ò', 'ó', 'ỏ', 'õ', 'ọ', 
        'ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ', 
        'ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ', 
        'ù', 'ú', 'ủ', 'ũ', 'ụ', 
        'ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự', 
        'ỳ', 'ý', 'ỷ', 'ỹ', 'ỵ', 
        'Ă', 'Â', 'Đ', 'Ê', 'Ô', 'Ơ', 'Ư'
    };



        private static char[] convertUnicodeTable;
        private static Dictionary<char, char> convertUnicodeTableSpecial;
        private static Dictionary<char, char> convertTCVNTable;

        private static char[] convertUnicodeFromASCTable;
        private static Dictionary<char, char> convertASCTable;

        private static char[] convertUnicodeFromVRETable;
        private static Dictionary<char, char> convertVRETable;


        static ConverterFont()
        {
            convertVRETable = new Dictionary<char, char>();
            convertTCVNTable = new Dictionary<char, char>();
            convertASCTable = new Dictionary<char, char>();
            convertUnicodeTable = new char[256];
            for (int i = 0; i < 256; i++)
                convertUnicodeTable[i] = (char)i;
            for (int i = 0; i < tcvnchars.Length; i++)
            {
                convertUnicodeTable[tcvnchars[i]] = unichars[i];
                convertTCVNTable.Add(unichars[i], tcvnchars[i]);
                convertASCTable[unichars[i]] = ascchars[i];
            }

            convertUnicodeFromVRETable = new char[9000];
            for (int i = 0; i < 256; i++)
            {
                convertUnicodeFromVRETable[i] = (char)i;
            }
            for (int i = 0; i < vrechars.Length; i++)
            {
                convertUnicodeFromVRETable[vrechars[i]] = unichars[i];
                convertVRETable.Add(unichars[i], vrechars[i]);
            }

            convertUnicodeFromASCTable = new char[256];
            for (int i = 0; i < 256; i++)
                convertUnicodeFromASCTable[i] = (char)i;

            //for (int i = 0; i < ascchars.Length; i++)
            //    convertUnicodeFromVRETable[ascchars[i]] = unichars[i];

            convertUnicodeTableSpecial = new Dictionary<char, char>();

            convertUnicodeTableSpecial.Add((char)416, 'ế');
            convertUnicodeTableSpecial.Add((char)771, 'ị');
            convertUnicodeTableSpecial.Add((char)803, 'ũ'); 
            convertUnicodeTableSpecial.Add((char)431, 'í');
            convertUnicodeTableSpecial.Add((char)777, 'ế');
            convertUnicodeTableSpecial.Add((char)259, 'ấ');
            convertUnicodeTableSpecial.Add((char)432, 'ý');
        }

        public static string TCVN3ToUnicode(string value)
        {
            char[] chars = value.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
                if (chars[i] < (char)256)
                {
                    chars[i] = convertUnicodeTable[chars[i]];
                }
                else
                {
                   if (convertUnicodeTableSpecial.ContainsKey(chars[i]))
                        chars[i] = convertUnicodeTableSpecial[chars[i]];
                }
            return new String(chars);
        }

        public static string UnicodeToTCVN3(string value)
        {
            char[] chars = value.ToCharArray();
            int i = 0, length = chars.Length;
            for (i = 0; i < length; i++)
                chars[i] = convertTCVNTable.ContainsKey(chars[i]) ? convertTCVNTable[chars[i]] : chars[i];
            return new string(chars);
        }

        public static string VREToUnicode(string value)
        {
            char[] chars = value.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
                if (chars[i] < (char)256)
                    chars[i] = convertUnicodeFromVRETable[chars[i]];
                else
                {
                    switch (chars[i])
                    {
                        case (char)8482:
                            chars[i] = 'Ă';
                            break;
                        case (char)353:
                            chars[i] = 'Â';
                            break;
                        case (char)376:
                            chars[i] = 'Đ';
                            break;
                        case (char)8250:
                            chars[i] = 'Ê';
                            break;
                        case (char)339:
                            chars[i] = 'Ô';
                            break;
                        case (char)382:
                            chars[i] = 'Ơ';
                            break;
                        case (char)341:
                            chars[i] = 'Ư';
                            break;
                        default:
                            break;

                    }
                }
            return new string(chars);
        }
        public static string UnicodeToVRE(string value)
        {
            char[] chars = value.ToCharArray();
            int i = 0, length = chars.Length;
            for (i = 0; i < length; i++)
                chars[i] = convertVRETable.ContainsKey(chars[i]) ? convertVRETable[chars[i]] : chars[i];
            return new string(chars);
        }

        public static string UnicodeToASC(string value)
        {
            char[] chars = value.ToCharArray();
            int i = 0, length = chars.Length;
            for (i = 0; i < length; i++)
                chars[i] = convertASCTable.ContainsKey(chars[i]) ? convertASCTable[chars[i]] : chars[i];
            return new string(chars);
        }
    }
}
